<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of friend_m
 *
 * @author Gidaff_02
 */
class Friend_m extends Model {

    function Friend_m() {
        parent::Model();
        $this->load->database();
    }

    function get_friends($account_id, $per_page, $off_set) {
        if ($account_id > 0) {
            $this->db->select('doggy_friends.account_id,doggy_profile.display_name,doggy_profile.avatar,doggy_friends.friend_id');
            $this->db->from('doggy_accounts ,doggy_friends ,doggy_profile');
            $this->db->where('doggy_friends.friend_id = doggy_accounts.id');
            $this->db->where('doggy_accounts.id = doggy_profile.account_id');
            $this->db->where('doggy_accounts.id <> ', $account_id);
            $this->db->where('doggy_friends.account_id', $account_id);
            $this->db->limit($per_page, $off_set);
            $query = $this->db->get();
            return $query->result_array();
        }
        return array();
    }

    function count_friends($account_id) {
        if ($account_id > 0) {
            $this->db->select('doggy_friends.account_id,doggy_profile.display_name,doggy_profile.avatar,doggy_friends.friend_id');
            $this->db->from('doggy_accounts ,doggy_friends ,doggy_profile');
            $this->db->where('doggy_friends.friend_id = doggy_accounts.id');
            $this->db->where('doggy_accounts.id = doggy_profile.account_id');
            $this->db->where('doggy_accounts.id <> ', $account_id);
            $this->db->where('doggy_friends.account_id', $account_id);
            $query = $this->db->get();
            return count($query->result_array());
        }
        return 0;
    }

    function list_random_account($id, $per_page, $off_set) {
        if ($id > 0) {
            $this->db->select('doggy_accounts.id,doggy_accounts.account_name,doggy_profile.display_name,doggy_profile.avatar');
            $this->db->from('doggy_accounts,doggy_profile');
            $this->db->where('doggy_accounts.id = doggy_profile.account_id');
            $this->db->where('doggy_accounts.id <> ', $id);
            $this->db->where('doggy_accounts.id NOT IN (Select doggy_friend_requests.to_account from doggy_friend_requests WHERE doggy_friend_requests.from_account=' . $id . ')');
            $this->db->where('doggy_accounts.id NOT IN (Select doggy_friends.friend_id from doggy_friends WHERE doggy_friends.account_id=' . $id . ')');
            $this->db->order_by('RAND()');
            $this->db->limit($per_page, $off_set);
            $query = $this->db->get();
            return $query->result_array();
        }
        return array();
    }

    function check_not_friend($friend_id,$doggy_id) {
        $this->db->select('doggy_accounts.id,doggy_accounts.account_name,doggy_profile.display_name,doggy_profile.avatar');
        $this->db->from('doggy_accounts,doggy_profile');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.id <> ', $friend_id);
        $this->db->where('doggy_accounts.id ', $doggy_id);
        $this->db->where('doggy_accounts.id NOT IN (Select doggy_friend_requests.to_account from doggy_friend_requests WHERE doggy_friend_requests.from_account=' . $friend_id . ')');
        $this->db->where('doggy_accounts.id NOT IN (Select doggy_friends.friend_id from doggy_friends WHERE doggy_friends.account_id=' . $friend_id . ')');
        $query = $this->db->get();
        return $query;
    }

    function count_random_account($id) {
        if ($id > 0) {
            $this->db->select('doggy_accounts.id,doggy_accounts.account_name,doggy_profile.display_name,doggy_profile.avatar');
            $this->db->from('doggy_accounts,doggy_profile');
            $this->db->where('doggy_accounts.id = doggy_profile.account_id');
            $this->db->where('doggy_accounts.id <> ', $id);
            $this->db->where('doggy_accounts.id NOT IN (Select doggy_friend_requests.to_account from doggy_friend_requests WHERE doggy_friend_requests.from_account=' . $id . ')');
            $this->db->where('doggy_accounts.id NOT IN (Select doggy_friends.friend_id from doggy_friends WHERE doggy_friends.account_id=' . $id . ')');
            $query = $this->db->get();
            return count($query->result_array());
        }
        return 0;
    }

    function add_friend_request($adder, $friend_id) {
        $data = array('from_account' => $adder, 'to_account' => $friend_id);
        return $this->db->insert('doggy_friend_requests', $data);
    }

    function count_friend_request($account_id) {
        $this->db->select('doggy_friend_requests.id,doggy_friend_requests.from_account,doggy_friend_requests.to_account,doggy_profile.display_name,doggy_profile.avatar');
        $this->db->from('doggy_friend_requests,doggy_accounts,doggy_profile');
        $this->db->where('doggy_friend_requests.from_account = doggy_accounts.id');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_friend_requests.to_account', $account_id);
        $query = $this->db->get();
        return count($query->result_array());
    }

    function list_friend_request($account_id, $per_page, $off_set) {
        $this->db->select('doggy_friend_requests.id,doggy_friend_requests.from_account,doggy_friend_requests.to_account,doggy_profile.display_name,doggy_profile.avatar');
        $this->db->from('doggy_friend_requests,doggy_accounts,doggy_profile');
        $this->db->where('doggy_friend_requests.from_account = doggy_accounts.id');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_friend_requests.to_account', $account_id);
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_friend_details($id) {
        $this->db->select('doggy_accounts.id,doggy_profile.fullname,doggy_profile.display_name,doggy_profile.birthday,
            doggy_profile.color,doggy_profile.sex,doggy_profile.avatar,doggy_profile.address,doggy_profile.mind_status,
            doggy_profile.self_description');
        $this->db->from('doggy_accounts,doggy_profile');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.id', $id);
        return $this->db->get();
    }

    function send_request($data) {
        return $this->db->insert('doggy_friend_requests', $data);
    }

    function add_friend($data) {
        return $this->db->insert('doggy_friends', $data);
    }

    function delete_friend_request($friend_id,$to_account) {
        return $this->db->delete('doggy_friend_requests', array('from_account' => $friend_id,'to_account'=>$to_account));
    }

}

?>
